//
//  LGPatientViewController.m
//  呼吸训练
//
//  Created by 1234 on 2024/5/8.
//
#import "LGPatientViewController.h"
#import "LGPatientDetailController.h"
#import "LGPatientTableViewCell.h"
#import "TUIDefine.h"
@interface LGPatientViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic , strong)UITextField *searchTF;
/**   */
@property (nonatomic , strong)UITableView *tableView;
/**   */
@property (nonatomic , strong)NSMutableArray *dataArray;
/**   */
@property (nonatomic , copy)NSString *pageNo;

@end

@implementation LGPatientViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self createSubView];
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self requestData];
    [self requestDoctorInfoData];
}

-(void)requestData{
    [RequestUtil withGET:@"bed/bunk/bunkUser" parameters:@{@"doctorId":kUserId} header:YES success:^(NSInteger code, NSString *message, id returnData) {
        self.isfirst = NO;
        [self.emptyView stopBufferAction];
        [self.tableView.mj_header endRefreshing];
        if(code == 200){
            self.dataArray = returnData;
        }
        [self.tableView reloadData];
        if(self.dataArray.count == 0){
            [self.emptyView showViewWithImage:@"emptyView2" content:@"暂无数据" offset:0];
        }
    } error:^(NSError *error) {
        self.isfirst = NO;
        [self.emptyView stopBufferAction];
        [self.tableView.mj_header endRefreshing];
        if(self.dataArray.count == 0){
            [self.emptyView showViewWithImage:@"emptyView2" content:@"暂无数据" offset:0];
        }
    }];
}

//修改腾讯云个人资料
-(void)requestDoctorInfoData{
    [RequestUtil withGET:@"/patient/doctorInfo" parameters:@{@"doctorId":kUserId} header:YES success:^(NSInteger code, NSString *message, id returnData) {
        if (code == 200) {
            NSDictionary *tempDic = returnData;
            __block NSString *realName = LGNSString(returnData[@"realName"]);
            __block NSString *headPic = LGNSString(returnData[@"avatar"]);
            [[V2TIMManager sharedInstance] getUsersInfo:@[kUserId] succ:^(NSArray<V2TIMUserFullInfo *> *infoList) {
                if (infoList.count>0) {
                    V2TIMUserFullInfo *userInfo = infoList[0];
                    userInfo.nickName = realName;
                    userInfo.faceURL = headPic;
                    [[V2TIMManager sharedInstance] setSelfInfo:userInfo succ:nil fail:nil];
                }
            } fail:nil];
            
        }
    } error:^(NSError *error) {
        
    }];
}


-(void)textFieldEditingChanged:(UITextField *)textField{
    
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    LGPatientTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.dataDic = self.dataArray[indexPath.row];
//    cell.type = self.type;
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    LGPatientDetailController *controller = [[LGPatientDetailController alloc]init];
    controller.patientid = LGNSString(self.dataArray[indexPath.row][@"id"]);
    controller.patientDic = self.dataArray[indexPath.row];
    controller.type = @"outside";
    [self.navigationController pushViewController:controller animated:YES];
}


-(void)createSubView{
    self.fd_prefersNavigationBarHidden = YES;
    self.view.backgroundColor = [UIColor colorWithString:@"#F3F1FC"];
    UIImageView *bgView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, Screen_W, viewPix(255))];
    bgView.image = [UIImage imageNamed:@"患者管理-首页背景"];
    [self.view addSubview:bgView];
    [self.view addSubview:self.searchTF];
    [self.view addSubview:self.tableView];
}


-(UITableView *)tableView{
    if(!_tableView){
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, statusBarHeight+viewPix(64), Screen_W, Screen_H-tabBarHeight-statusBarHeight-viewPix(74)) style:UITableViewStylePlain];
        [_tableView registerClass:[LGPatientTableViewCell class] forCellReuseIdentifier:@"cell"];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.rowHeight = UITableViewAutomaticDimension;
        _tableView.backgroundColor = [UIColor clearColor];
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.tableHeaderView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, Screen_W, viewPix(5))];
        _tableView.mj_header = [MJRefreshHeader headerWithRefreshingTarget:self refreshingAction:@selector(requestData)];
    }
    return _tableView;
}


-(UITextField *)searchTF{
    if(!_searchTF){
        _searchTF = [[UITextField alloc]initWithFrame:CGRectMake(viewPix(16), statusBarHeight+viewPix(18), Screen_W-viewPix(32), viewPix(36))];
        _searchTF.tintColor = [UIColor colorWithString:@"#2B2A37"];
        _searchTF.textColor = [UIColor colorWithString:@"#2B2A37"];
        _searchTF.backgroundColor = [UIColor whiteColor];
        _searchTF.textAlignment = NSTextAlignmentLeft;
        _searchTF.keyboardType = UIKeyboardTypeDefault;
        _searchTF.placeholder = @"请输入患者姓名/病案号进行查找";
        _searchTF.cornerRidus = viewPix(18);
        _searchTF.font = LGFont(14);
        [_searchTF addTarget:self action:@selector(textFieldEditingChanged:) forControlEvents:UIControlEventEditingChanged];
        UIView *leftView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, viewPix(36), viewPix(36))];
        UIImageView *iconView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"searchGray16"]];
        iconView.frame = CGRectMake(viewPix(10), viewPix(10), viewPix(16), viewPix(16));
        [leftView addSubview:iconView];
        _searchTF.leftView = leftView;
        _searchTF.leftViewMode = UITextFieldViewModeAlways;
    }
    return _searchTF;
}


@end
